﻿using System;
using System.Configuration;
using System.Web;
using Shuba.Model;
using System.Collections.Generic;
using System.Web.UI.WebControls;

namespace Shuba.Web.master
{
    public partial class regional_plan_add : basePage
    {
        string msg = "";

        private void BindData()
        {
            Shuba.Business.RegionalTarget _RegionalTarget = new Shuba.Business.RegionalTarget();
            List<RegionalTargetInfo> list;

            list = _RegionalTarget.GetList(ddlYear.Text, Session["UnitID"].ToString());
            gvList.DataSource = list;
            gvList.DataBind();

            if (gvList.Rows.Count > 0)
            {
                btnSubmit.Enabled = true;
                btnCancel.Enabled = true;
            }
            else
            {
                btnSubmit.Enabled = false;
                btnCancel.Enabled = false;
            }
        }

        protected void ddlYear_Change(object sender, EventArgs e)
        {
            BindData();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            //this.Form.DefaultButton = btnSubmit.UniqueID;

            if (!Page.IsPostBack)
            {
                string acl = GetUserAccess("TARGET_REGIONAL");

                if (!IsUserAdd(acl) && !IsUserEdit(acl))
                {
                    msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                    AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
                }

                LabelTitle.Text = GetPageTitle("TARGET_REGIONAL");

                InitYearList();

                ddlYear.SelectedIndex = ddlYear.Items.IndexOf(ddlYear.Items.FindByValue(ProcessingDate().Year.ToString()));

                BindData();
            }
        }

        protected void InitYearList()
        {
            Int32 NextYear = Convert.ToInt32(ProcessingDate().Year.ToString()) + 4;
            Int32 LastYear = Convert.ToInt32(ProcessingDate().Year.ToString()) - 1;

            for (var n = LastYear; n < NextYear; n++)
            {
                ddlYear.Items.Add(n.ToString());
            }
        }
        
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string ActType = "INSERT";
            Int64 TargetAmount = 0, TargetAmountReal = 0;
            Int32 TargetNoa = 0, TargetNoaReal = 0;

            Shuba.Business.RegionalTarget _RegionalTarget = new Shuba.Business.RegionalTarget();
            Shuba.Model.RegionalTargetInfo bi = new Shuba.Model.RegionalTargetInfo();

            foreach (GridViewRow row in gvList.Rows)
            {
                TextBox txtTargetAmount = (TextBox)row.FindControl("txtTargetAmount");
                TextBox txtTargetNoa = (TextBox)row.FindControl("txtNoa");
                TextBox txtTargetAmountReal = (TextBox)row.FindControl("txtTargetAmountReal");
                TextBox txtTargetNoaReal = (TextBox)row.FindControl("txtNoaReal");

                RangeValidator AmountValidator = (RangeValidator)row.FindControl("RangeValidatorAmount");
                RangeValidator NoaValidator = (RangeValidator)row.FindControl("RangeValidatorNoa");
                RangeValidator AmountValidator2 = (RangeValidator)row.FindControl("RangeValidatorAmount2");
                RangeValidator NoaValidator2 = (RangeValidator)row.FindControl("RangeValidatorNoa2");

                string id = Convert.ToString(gvList.DataKeys[row.RowIndex].Value.ToString());

                if (txtTargetAmount.Text != "") TargetAmount = Convert.ToInt64(txtTargetAmount.Text); else TargetAmount = 0;
                if (txtTargetNoa.Text != "") TargetNoa = Convert.ToInt32(txtTargetNoa.Text); else TargetNoa = 0;

                if (txtTargetAmountReal.Text != "") TargetAmountReal = Convert.ToInt64(txtTargetAmountReal.Text); else TargetAmountReal = 0;
                if (txtTargetNoaReal.Text != "") TargetNoaReal = Convert.ToInt32(txtTargetNoaReal.Text); else TargetNoaReal = 0;


                //if ((TargetAmount > 0 && TargetNoa > 0) || (TargetAmountReal > 0 && TargetNoaReal > 0))
                //{
                    bi.RegionalID = id;
                    bi.TargetYear = ddlYear.Text;
                    bi.TargetAmount = TargetAmount;
                    bi.Noa = TargetNoa;
                    bi.TargetAmountReal = TargetAmountReal;
                    bi.NoaReal = TargetNoaReal;
                    bi.EntryBy = HttpContext.Current.User.Identity.Name;
                    bi.EntryDate = ProcessingDate();
                    bi.UpdateBy = HttpContext.Current.User.Identity.Name;
                    bi.UpdateDate = ProcessingDate();
                    bi.ActType = ActType;

                    _RegionalTarget.Save(bi);

                    AmountValidator.Visible = false;
                    NoaValidator.Visible = false;
                    AmountValidator2.Visible = false;
                    NoaValidator2.Visible = false;

                    AddLog(ActType, ActType + " REGION TARGET PLAN");
                //}

            }

            msg = this.GetGlobalResourceObject("Messages", "SaveAlert").ToString();
            AlertAndRedirect(msg, "regional_plan_list.aspx");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("regional_plan_list.aspx");
        }
    }
}
